<?php
class NavModel extends Model{
	protected $_auto = array(
		array('path','tclm',3,'callback'),
	);
	function tclm(){
		$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;
		if($pid==0) return 0;
		$list=$this->where("id=$pid")->find();
		$data=$list['path'].'-'.$list['id'];
		return $data;
	}
    public function nlist($type,$id,$num,$length,$conlen,$order,$page){
		$nlist = array();
		if($type == 1){
			if($id==-1)  $id = 0;
			$nlist = $this->where("type=1 AND state=0 AND pid={$id}")->order('taxis ASC,id ASC')->select();
			if(C('ISHTML')){
				foreach($nlist as $key=>$value){
					$nlist[$key]['url'] = C('URL').'/'.$value['dir'];
					$nlist[$key]['fullurl'] = '<a href="'.C('URL').'/'.$value['dir'].'" title="'.$value['name'].'">'.$value['name'].'</a>';
				}
			}else{
				foreach($nlist as $key=>$value){
					$nlist[$key]['url'] = C('URL').'/index.php/Index/nlist/id/'.$value['id'];
					$nlist[$key]['fullurl'] = '<a href="'.C('URL').'/index.php/Index/nlist/id/'.$value['id'].'" title="'.$value['name'].'">'.$value['name'].'</a>';
				}
			}
			if($num != -1) $nlist = array_slice($nlist,0,$num);
			return $nlist;
		}else if($type == 2){
			if($id==0)  $id = (int)$_GET['id'];
			$nlist = $this->where("type=1 AND state=0 AND id={$id}")->select();
			if(C('ISHTML')){
				foreach($nlist as $key=>$value){
					$nlist[$key]['url'] = C('URL').'/'.$value['dir'];
					$nlist[$key]['fullurl'] = '<a href="'.C('URL').'/'.$value['dir'].'" title="'.$value['name'].'">'.$value['name'].'</a>';
				}
			}else{
				foreach($nlist as $key=>$value){
					$nlist[$key]['url'] = C('URL').'/index.php/Index/nlist/id/'.$value['id'];
					$nlist[$key]['fullurl'] = '<a href="'.C('URL').'/index.php/Index/nlist/id/'.$value['id'].'" title="'.$value['name'].'">'.$value['name'].'</a>';
				}
			}
			return $nlist;
		}else if($type == 3){
			$list = $this->where("id={$id} OR type=1 AND state=0 AND concat('-',path,'-') like '%-{$id}-%'")->field('id,name,dir,listname,conname')->select();
			if($order == 1){
				$i = 0;
				foreach($list as $key=>$value){
					$nid = $value['id'];
					$lists = M()->Table(array(C('DB_PREFIX').'news'=>'news',C('DB_PREFIX').'user'=>'user'))->where("news.pid = $nid AND news.state = 1 AND news.uid = user.uid")->select();
					foreach($lists as $k=>$v){
						if(C('ISHTML')){
							$conname = str_replace('{id}',$v['id'],$value['conname']);
							$conname = str_replace('{Y}',date('Y',$v['time']),$conname);
							if(empty($v['url'])){
								$v['url']= C('URL').'/'.$value['dir'].'/'.$conname;
							}	
							$v['nurl'] = C('URL').'/'.$value['dir'];
						}else{
							if(empty($v['url'])){
								$v['url']= C('URL').'/index.php/Index/news/id/'.$v['id'];
							}
							$v['nurl']= C('URL').'/index.php/Index/nlist/id/'.$value['id'];
						}
						$v['fullurl'] = '<a href="'.$v['url'].'" title="'.$v['title'].'">'.$v['title'].'</a>';
						$v['fullnurl'] = '<a href="'.$v['nurl'].'" title="'.$value['name'].'">'.$value['name'].'</a>';	
						$v['name'] = $value['name'];
						$v['date'] = date('Y-m-d H:i',$v['time']);
						$v['title'] = msubstr($v['title'],0,$length,'utf-8',false);
						$v['content'] = msubstr($v['content'],0,$conlen,'utf-8',false);
						$v['spic'] = C('URL').'/Public/news/s_'.$v['pic'];
						$v['fullspic'] = '<img src="'.$v['spic'].'" title="'.$v['title'].'" alt="'.$v['title'].'" border="0"/>';
						$v['mpic'] = C('URL').'/Public/news/m_'.$v['pic'];
						$v['fullmpic'] = '<img src="'.$v['mpic'].'" title="'.$v['title'].'" alt="'.$v['title'].'" border="0"/>';
						if($num != -1 && count($nlist)>=$num) break;
						$nlist[] = $v;
					}
					if($id == $nid) $dir = $i;
					$i++;
				}
				$nlist = $this->multi_array_sort($nlist,'time',SORT_DESC);
			}elseif($order==2){
				$i = 0;
				foreach($list as $key=>$value){
					$nid = $value['id'];
					$lists = M('News')->where("pid = {$nid} AND state=1")->order('click DESC,taxis ASC,time DESC')->select();
					foreach($lists as $k=>$v){
						if(C('ISHTML')){
							$conname = str_replace('{id}',$v['id'],$value['conname']);
							$conname = str_replace('{Y}',date('Y',$v['time']),$conname);
							if(empty($v['url'])){
								$v['url']= C('URL').'/'.$value['dir'].'/'.$conname;
							}
							$v['nurl'] = C('URL').'/'.$value['dir'];
						}else{
							if(empty($v['url'])){
								$v['url']= C('URL').'/index.php/Index/news/id/'.$v['id'];
							}	
							$v['nurl']= C('URL').'/index.php/Index/nlist/id/'.$value['id'];
						}
						$v['fullurl'] = '<a href="'.$v['url'].'" title="'.$v['title'].'">'.$v['title'].'</a>';
						$v['fullnurl'] = '<a href="'.$v['nurl'].'" title="'.$value['name'].'">'.$value['name'].'</a>';	
						$v['name'] = $value['name'];
						$v['date'] = date('Y-m-d H:i',$v['time']);
						$v['title'] = msubstr($v['title'],0,$length,'utf-8',false);
						$v['content'] = msubstr(strip_tags($v['content']),0,$conlen,'utf-8',false);
						$v['spic'] = C('URL').'/Public/news/s_'.$v['pic'];
						$v['fullspic'] = '<img src="'.$v['spic'].'" title="'.$v['title'].'" alt="'.$v['title'].'" border="0"/>';
						$v['mpic'] = C('URL').'/Public/news/m_'.$v['pic'];
						$v['fullmpic'] = '<img src="'.$v['mpic'].'" title="'.$v['title'].'" alt="'.$v['title'].'" border="0"/>';
						if($num != -1 && count($nlist)>=$num) break;
						$nlist[] = $v;
						if($id == $nid) $dir = count($nlist)-1;
					}
					if($id == $nid) $dir = $i;
					$i++;
				}
				$nlist = $this->multi_array_sort($nlist,'click',SORT_DESC);
			}
			if($page==-1){
				$listname = C('URL').'/'.$list[$dir]['dir'].'/'.array_shift(explode('{page}',str_replace('{lid}',$list[0]['id'],$list[0]['listname'])));
				return $this->page(0,$nlist,$listname);
			}else{
				return $nlist;
			}
		}else{
			exit ('type要么为1,要么为2.要么为3.你只能选这三个');
		}
	}
	public function page($type,$list,$listname){
		import("@.ORG.Page");       //导入分页类
		if($type==0) $page = C('listnews');
		else $page = C('listproduct');
		$total = count($list);
		if(!C('ISHTML')) $p = new Page($total,$page);
		else $p = new Page($total,$page,$listname);
		$list = array_slice($list,$p->firstRow,$p->listRows);
		$p->setConfig('prev','<<上一页');
		$p->setConfig('next','下一页>>');
		$p->setConfig('first','<<第一页'); 
        $p->setConfig('last','最后一页>>');
		$p->setConfig('theme','%totalRow% %header% %nowPage%/%totalPage% 页 %first% %prePage% %linkPage% %nextPage% %end%');
		$page = $p->show();
		if($type==0){
			$data['news'] = $list;
			$data['page'] = $page;
		}else{
			$data['product'] = $list;
			$data['page'] = $page;
		}
		return $data;
	}
	public function multi_array_sort($multi_array,$sort_key,$sort=SORT_ASC){
		if(is_array($multi_array)){
			foreach ($multi_array as $row_array){ 
				if(is_array($row_array)){
					$key_array[] = $row_array[$sort_key];
				}else{ 
					return false; 
				} 
			}
		}else{ 
			return false; 
		}
		array_multisort($key_array,$sort,$multi_array); 
		return $multi_array; 
	}
}
?>